using UnityEngine;

namespace NodeCanvas
{
	[AddComponentMenu("")]
	public class ConditionalConnection : Connection, ITaskAssignable
	{
		[SerializeField]
		private Object _condition;

		public Task task
		{
			get
			{
				return condition;
			}
			set
			{
				condition = (ConditionTask)value;
			}
		}

		public Object serializedTask
		{
			get
			{
				return _condition;
			}
		}

		public ConditionTask condition
		{
			get
			{
				return _condition as ConditionTask;
			}
			set
			{
				_condition = value;
				if (value != null)
				{
					value.SetOwnerSystem(base.graph);
				}
			}
		}

		protected override Status OnExecute(Component agent, Blackboard blackboard)
		{
			if (!condition || condition.CheckCondition(agent, blackboard))
			{
				return base.targetNode.Execute(agent, blackboard);
			}
			base.targetNode.ResetNode();
			return Status.Failure;
		}

		public bool CheckCondition()
		{
			return CheckCondition(base.graphAgent, base.graphBlackboard);
		}

		public bool CheckCondition(Component agent)
		{
			return CheckCondition(agent, base.graphBlackboard);
		}

		public virtual bool CheckCondition(Component agent, Blackboard blackboard)
		{
			if (base.isActive && (!condition || condition.CheckCondition(agent, blackboard)))
			{
				return true;
			}
			base.connectionStatus = Status.Failure;
			return false;
		}
	}
}
